from utils.format import dict_list_rename_key
from db.db_customer import DBCustomer
from db.db_bill import DBBill
from db.db_sale import DBSale
from boot import List, Dict, IMG_ROOT_DIR, BillCurrentFilter, BillType, SaleCurrentFilter, logger
import json


__all__ = ["cust_content_dict_map", "bill_content_dict_map", "sale_content_dict_map"]


CUST_SRC_DICT_KEYS_LIST: List[str]= ["cust_id", "cust_icon", "cust_name", "cust_company", "cust_tel"]
BILL_SRC_DICT_KEYS_LIST: List[str]= ["bill_id", "bill_type", "cust_id", "bill_datetime", "bill_amount"]
SALE_SRC_DICT_KEYS_LIST: List[str]= ["sale_id", "icon_path", "cust_id", "sale_datetime", "sale_number"]
DST_DICT_KEYS_LIST: List[str] = ["item_id", "icon_path", "primary_name", "secondary_name", "back_name"]


# 客户信息内容字典映射
def cust_content_dict_map(method):
    def wrapper(self, *args, **kwargs):
        with DBCustomer() as cust_db:
            info_all_dict_list = cust_db.get_all_info()
            new_dict_list = dict_list_rename_key(
                info_all_dict_list,
                CUST_SRC_DICT_KEYS_LIST,
                DST_DICT_KEYS_LIST
            )
        method(self, new_dict_list)
        return None
    return wrapper

# 回款信息内容字典映射
def bill_content_dict_map(method):
    def wrapper(self, *args, **kwargs):
        with DBCustomer() as cust_db:
            with DBBill() as bill_db:
                # 1.获取原始数据
                info_all_dict_list = bill_db.get_all_info(BillCurrentFilter.cust_id)
                # 2.对bill_type, cust_id进行内容替换
                for dict_item in info_all_dict_list:
                    dict_item["bill_type"] = IMG_ROOT_DIR + "/pay/" + BillType.type_dict[dict_item["bill_type"]]
                    dict_item["cust_id"] = cust_db.get_name_by_id(dict_item["cust_id"])

                new_dict_list = dict_list_rename_key(
                    info_all_dict_list,
                    BILL_SRC_DICT_KEYS_LIST,
                    DST_DICT_KEYS_LIST
                )
        method(self, new_dict_list)
        return None
    return wrapper


# 销货清单信息内容字典
def sale_content_dict_map(method):
    def wrapper(self, *args, **kwargs):
        with DBCustomer() as cust_db:
            with DBSale() as sale_db:
                # 1.获取原始数据
                info_all_dict_list = sale_db.get_all_info(SaleCurrentFilter.cust_id)
                # 2.加入icon_path键和sale_number键, 替换cust_id
                for dict_item in info_all_dict_list:
                    # 2.1新增icon_path键
                    dict_item["icon_path"] = IMG_ROOT_DIR + "/sale/" + "list.jpg"
                    # 2.2新增sale_number（件数）键
                    sale_number = 0
                    # logger.info(dict_item["sale_list"])
                    for row in json.loads(dict_item["sale_list"])["content"]:
                        sale_number += int(row[1]) if row[1].replace(" ", "") != "" else 0
                    dict_item["sale_number"] = str(sale_number)
                    # 2.3替换cust_id
                    dict_item["cust_id"] = cust_db.get_name_by_id(dict_item["cust_id"])
                # 3.映射
                new_dict_list = dict_list_rename_key(
                    info_all_dict_list,
                    SALE_SRC_DICT_KEYS_LIST,
                    DST_DICT_KEYS_LIST
                )
        method(self, new_dict_list)
        return None
    return wrapper